package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"employee:view","员工页面"},logical = Logical.OR)
    public String view() throws Exception{
        return "employee/employee";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"employee:saveOrUpdate","员工添加/编辑"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Employee entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            employeeService.saveOrUpdate(entity );
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    @RequestMapping("changePwd")
    @ResponseBody
    @RequiresPermissions(value = {"employee:changePwd","员工密码重置"},logical = Logical.OR)
    public JsonResult changePwd(String password, Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            employeeService.changePwd(password,id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("status")
    @ResponseBody
    @RequiresPermissions(value = {"employee:status","员工状态修改"},logical = Logical.OR)
    public JsonResult status(Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            employeeService.changeState(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(EmployeeQueryObject qo) throws Exception{
        return employeeService.query(qo);
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置文件下载响应头
        response.setHeader("Content-Disposition","attachment;filename=employee2.xls");
        //创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("list");
        //设置标题
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        List<Employee> employees = employeeService.selectAll();
        for(int i = 0; i < employees.size() ; i++){
            Employee employee = employees.get(i);
            //创建行
            row = sheet.createRow(i + 1);
            //设置单元格内容
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getRealname());
        }
        //写入数据
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("importXls")
    public void importXls(MultipartFile file, HttpServletResponse response) throws IOException {
        //创建excel,指定输入流
        InputStream inputStream = file.getInputStream();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

        Sheet sheet = workbook.getSheetAt(0);
        //获取最后一行索引
        int lastRowNum = sheet.getLastRowNum();
        for(int i = 0; i <= lastRowNum; i++){
            Employee employee = new Employee();
            Row row = sheet.getRow(i);
            employee.setUsername((row.getCell(0)).getStringCellValue());
            employee.setRealname((row.getCell(1)).getStringCellValue());
            employeeService.insert(employee);
        }
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Employee> listAll() throws Exception{
        return employeeService.listAll();
    }

}
